package com.example.demo.config;

import com.example.demo.controller.data.ReturnData;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 处理全局异常类
 */
@ControllerAdvice
public class HandlerException {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ReturnData> handle(Exception e){

        // 判断是不是请求参数错误
        // (自定义 是否为 请求参数类型)
        if(e instanceof IllegalStateException){
            return ResponseEntity.ok(ReturnData.error("请求参数错误",e.toString()));
        }
        return ResponseEntity.ok(ReturnData.error("服务器异常",e.toString()));
    }

}
